package com.tron.sync.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tron.sync.entities.ElkUserInfo;
import com.tron.sync.mapper.ElkUserInfoMapper;
import com.tron.sync.syncwfinfoindex.PlatformClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ElkUserInfoServiceImpl extends ServiceImpl<ElkUserInfoMapper, ElkUserInfo> implements ElkUserInfoService {

    @Autowired
    PlatformClient platformClient;


    @Override
    public ElkUserInfo getUserCenterInfo(String token, String userId) {
        final String userInfo = platformClient.getUserInfo(userId, token);
        final JSONObject jsonObject = JSON.parseObject(userInfo);
        final Integer state = jsonObject.getInteger("state");
        ElkUserInfo elkUserInfo = new ElkUserInfo();
        if (state == 200) {
            final JSONObject data = jsonObject.getJSONObject("data");
            final String userName = data.getString("userName");
            final String orgId = data.getString("orgId");
            final String orgName = data.getString("orgName");
            final String deptId = data.getString("deptId");
            final String deptName = data.getString("deptName");
            elkUserInfo.setUserId(userId);
            elkUserInfo.setS_userName(userName);
            elkUserInfo.setS_orgId(orgId);
            elkUserInfo.setS_orgName(orgName);
            elkUserInfo.setS_deptId(deptId);
            elkUserInfo.setS_deptName(deptName);
        }else {
            elkUserInfo = null;
        }
        return elkUserInfo;
    }

    // 保存用户中心相关用户数据
    @Override
    public ElkUserInfo saveElkUserInfoBySyndaUser(String userId,String token) {
        // 根据userId 获取用户中心用户信息
        /**
         * {"state":200,"request":null,"message":"","cause":"","variables":{},"data":{"pk":"","name":"高倩","ip":null,"createBy":null,"createTime":"2020-12-08 17:12:19","updateBy":null,"updateTime":"2021-01-20 16:08:36","tenantId":null,"dataStatus":null,"dbtype":null,"id":"785916685289783296","partyType":null,"alias":null,"parentId":null,"path":null,"depth":null,"sn":null,"extValue":null,"status":"actived","subCount":0,"profile":null,"gender":"female","email":null,"address":null,"mobile":"18314325210","qq":null,"photo":null,"positions":null,"groupID":"785916005351161856","wcAccount":null,"userIdentity":"11000","secretLevel":0,"idNo":"530121199803080924","caNo":"","type":"0","rank":null,"idPhoto":"","telephone":"-","insidePhone":"-","job":null,"userNumber":"","category":"T2","pinyin":"gq","birthday":"1998-03-08","hiredate":null,"joinPartyDate":null,"sort":16,"lockMode":null,"lockTime":null,"version":"0","account":"530121199803080924","isSuper":"N","orgName":"云南省公安厅警令部","isMainPost":null,"isPrincipal":null,"pinYinHeadChar":"gq","source":null,"manager":false},"errorCode":0,"code":200,"msg":""}
         */
        final String jsonString = platformClient.getUserInfo(userId, token);
        final JSONObject jsonObject = JSONObject.parseObject(jsonString);
        final Integer state = jsonObject.getInteger("state");
        if (state.equals(200)) {
            final JSONObject data = jsonObject.getJSONObject("data");
            final String syndaUserName = data.getString("name");
            data.getString("");
        }
        ElkUserInfo byId = getById(userId);
//        if (null != byId) {
//            byId.setS_userName(syndaUserName);
//            byId.setS_orgId(syndaOrgId);
//            byId.setS_orgName(syndaOrgName);
//            byId.setS_deptId(syndadeptId);
//            byId.setS_deptName(syndaDeptName);
//            updateById(byId);
//        }else {
//            byId = new ElkUserInfo();
//            byId.setUserId(userId);
//            byId.setS_userName(syndaUserName);
//            byId.setS_orgId(syndaOrgId);
//            byId.setS_orgName(syndaOrgName);
//            byId.setS_deptId(syndadeptId);
//            byId.setS_deptName(syndaDeptName);
//            save(byId);
//        }
        return byId;
    }

    // 保存全文检索相关用户数据
    @Override
    public ElkUserInfo saveElkUserInfoByElkUser(String userId, String elkUserName, String elkOrgId, String elkOrgName, String elkDeptId, String elkDeptName) {
        ElkUserInfo byId = getById(userId);
        if (null != byId) {
            byId.setUserName(elkUserName);
            byId.setOrgId(elkOrgId);
            byId.setOrgName(elkOrgName);
            byId.setDeptId(elkDeptId);
            byId.setDeptName(elkDeptName);
            updateById(byId);
        }else {
            byId = new ElkUserInfo();
            byId.setUserId(userId);
            byId.setUserName(elkUserName);
            byId.setOrgId(elkOrgId);
            byId.setOrgName(elkOrgName);
            byId.setDeptId(elkDeptId);
            byId.setDeptName(elkDeptName);
            save(byId);
        }
        return byId;
    }
}
